Skip to content

Add @res.hoistedFunction support for flat JS export - #8402

Open
cknitt wants to merge 3 commits into
masterfrom
hoisted-functions
Open

Add @res.hoistedFunction support for flat JS export#8402
cknitt wants to merge 3 commits into
masterfrom
hoisted-functions

Conversation

@cknitt

@cknitt cknitt commented May 1, 2026

Copy link
Copy Markdown
Member

Summary

Adds @res.hoistedFunction, a compiler-supported attribute for exporting nested module functions through flat JS exports.

A function defined inside an exported module can now be marked as hoisted. The compiler keeps the normal nested module shape, but also emits a root-level alias/export for the function and records that alias in .cmj metadata so downstream modules can import it directly.

Motivation

Nested module functions are normally emitted and consumed through property access:

// Producer.res
module A = {
  module B = {
    let make = () => "ok"
  }
}
// consumer output
Producer.A.B.make()

This is the first step for my take on #8293. That work needs generated JSX code for React Server Components to expose nested component functions through stable flat JS exports:

Producer.A$B$make()

Because producers and consumers are compiled separately, the consumer cannot infer from the source path alone whether Producer.A.B.make also has a flat export. The producer therefore records hoisted paths in .cmj metadata, and consumers use that metadata to emit the flat import when it is available.

Design

Source Marker

The feature introduces:

@res.hoistedFunction
let make = () => ...

The attribute currently applies only to function bindings. If it is placed on a non-function value or an external declaration, the compiler reports a misplaced-attribute warning.

The attribute deliberately applies only to nested module function bindings with a fixed exported path, matching the intended use case. Local functions, functions declared directly at the file root, local modules, and functor bodies cannot form such an export and produce a misplaced-attribute warning.

Producer Output

When an exported module contains a hoisted nested function, the compiler keeps the original module structure and adds a root-level alias.

For a source path like:

A.B.make

the compiler emits a flat export:

A$B$make

The nested function remains available at its normal path, while the flat alias is exported as a separate JS value.

Path Identity and Exotic Identifiers

The flat JavaScript name cannot safely identify the original source path by itself.

Representing a nested path by joining its segments with $ would be ambiguous because ReScript allows escaped identifiers containing $. For example, these are distinct source paths:

A.B.make
A.\"B$make"

However, both have the same natural flat JavaScript spelling:

A$B$make

Encoding the path segments in the generated export name would remove the ambiguity, but it would also produce surprising JavaScript names and prevent exotic identifiers from retaining their intended spelling.

Instead, the implementation separates source-path identity from the generated JavaScript name:

  • .cmj metadata stores the exact source path as a list of segments.
  • The generated export retains its natural flat JavaScript name.

Consumers therefore look up hoisted exports using the structural source path, while the generated JavaScript remains readable and predictable.

If two hoisted paths produce the same JavaScript export name, or if that name conflicts with an existing top-level binding, compilation fails with a clear error.

.cmj Metadata

The .cmj format stores each hoisted export as:

type hoisted_export = {
  path: string list;
  name: string;
}

For example, A.B.make is represented as:

{
  path: ["A", "B", "make"];
  name: "A$B$make";
}

The path is used for exact source-level lookup. The name is the compiler identifier used for the flat JavaScript export.

This metadata remains separate from the regular values table, which continues to hold arity and cross-module optimization information.

Consumer Lookup

When compiling a cross-module nested read such as:

Producer.A.B.make

normal compilation looks up the first field, A, in the producer’s .cmj and emits the remaining property accesses:

Producer.A.B.make

For a nested read, the compiler now also reconstructs the exact source path and checks the producer’s hoisted export metadata.

If the path is present, it uses the recorded flat export name:

Producer.A$B$make

Otherwise, it falls back to the normal property chain.

Because lookup uses the structural path rather than a $-joined key, exotic identifiers cannot accidentally resolve to another binding’s hoisted export.

Performance

Modules without @res.hoistedFunction require only a shallow, allocation-free scan of their top-level bindings.

For each annotated function, the generated JavaScript contains one additional alias assignment and export. Calls use the flat export directly; there is no runtime path lookup or encoding.

Consumer lookup is compile-time only and scans the dependency’s normally very small list of hoisted exports.

Tests

Adds coverage for:

  • several levels of nested modules;
  • mutually recursive modules;
  • function bindings with explicit type annotations;
  • hoisted and non-hoisted functions in the same modules;
  • cross-module access through flat exports;
  • escaped keyword, operator, and $ identifiers;
  • structurally distinct paths with the same flattened spelling;
  • collisions between hoisted exports;
  • collisions with existing top-level bindings;
  • invalid attribute payloads and placements where no flat export can be generated;
  • executed runtime behavior of the generated JavaScript.

@cknitt

cknitt commented May 1, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6dfddc0098

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread compiler/core/js_cmj_format.ml Outdated
@pkg-pr-new

pkg-pr-new Bot commented May 1, 2026

Copy link
Copy Markdown

Open in StackBlitz

rescript

npm i https://pkg.pr.new/rescript-lang/rescript@8402

@rescript/belt

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/belt@8402

@rescript/darwin-arm64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/darwin-arm64@8402

@rescript/darwin-x64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/darwin-x64@8402

@rescript/linux-arm64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/linux-arm64@8402

@rescript/linux-x64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/linux-x64@8402

@rescript/runtime

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/runtime@8402

@rescript/win32-x64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/win32-x64@8402

commit: 46cebb7

@cknitt

cknitt commented Aug 22, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: da4b5b3307

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread compiler/core/lam_compile_main.ml
Comment thread compiler/ml/translcore.ml Outdated
@codecov

codecov Bot commented Aug 22, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 87.75510% with 24 lines in your changes missing coverage. Please review.
✅ Project coverage is 75.98%. Comparing base (cf8dd8c) to head (46cebb7).

Files with missing lines Patch % Lines
compiler/ml/translmod.ml 74.57% 15 Missing ⚠️
compiler/core/lam_compile_main.ml 91.04% 6 Missing ⚠️
compiler/core/lam_compile.ml 96.42% 1 Missing ⚠️
compiler/core/lam_compile_env.ml 90.00% 1 Missing ⚠️
compiler/ml/translcore.ml 93.75% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #8402      +/-   ##
==========================================
+ Coverage   75.95%   75.98%   +0.02%     
==========================================
  Files         474      474              
  Lines       62905    63077     +172     
==========================================
+ Hits        47779    47928     +149     
- Misses      15126    15149      +23     
Files with missing lines Coverage Δ
compiler/core/js_cmj_format.ml 92.45% <100.00%> (+0.61%) ⬆️
compiler/core/lam_stats_export.ml 90.69% <100.00%> (ø)
compiler/ext/ident.ml 87.61% <100.00%> (+0.36%) ⬆️
compiler/ml/translattribute.ml 74.60% <100.00%> (+3.17%) ⬆️
compiler/core/lam_compile.ml 85.57% <96.42%> (+0.14%) ⬆️
compiler/core/lam_compile_env.ml 90.90% <90.00%> (-1.40%) ⬇️
compiler/ml/translcore.ml 90.53% <93.75%> (+0.10%) ⬆️
compiler/core/lam_compile_main.ml 95.32% <91.04%> (-1.98%) ⬇️
compiler/ml/translmod.ml 90.59% <74.57%> (-5.45%) ⬇️

... and 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions

Copy link
Copy Markdown

@cknitt
cknitt force-pushed the hoisted-functions branch from da4b5b3 to 8595ead Compare August 22, 2026 14:11
@cknitt

cknitt commented Aug 22, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8595ead79d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread compiler/ml/translcore.ml Outdated
Comment on lines +616 to +622
match pat.pat_desc with
| Tpat_var (id, _) ->
if allow_js_hoist then Ident.make_js_hoisted id
else
Location.prerr_warning loc
(Warnings.Misplaced_attribute hoisted_function_attr_name)
| _ -> ())

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Mark type-constrained function identifiers

When a hoisted binding has an explicit type annotation, such as @res.hoistedFunction let make: unit => string = () => "ok", type checking represents the variable as Tpat_alias ({pat_desc = Tpat_any}, id, _), not Tpat_var. This fallback therefore silently skips the attribute, producing neither the flat export nor a warning. Handle the alias form already accepted by transl_let for recursive bindings.

Useful? React with 👍 / 👎.

Comment thread compiler/ml/translmod.ml
Comment on lines +370 to +372
( Translcore.transl_let
~allow_js_hoist:(not (is_top rootpath))
rec_flag pat_expr_list body,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Reject hoists without an exportable module path

For a function annotated inside a local module or a functor body, rootpath is None or a Papply, so this condition enables hoisting even though no fixed top-level export path exists. js_hoisted_aliases later scans only exported top-level module blocks, causing the marker to be silently discarded with neither the promised flat export nor the misplaced-attribute warning. Only enable the attribute when the enclosing path can actually be materialized, or diagnose these contexts.

Useful? React with 👍 / 👎.

Signed-off-by: Christoph Knittel <ck@cca.io>
@cknitt
cknitt force-pushed the hoisted-functions branch from 8595ead to 3c50e90 Compare August 22, 2026 14:35
@cknitt

cknitt commented Aug 22, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3c50e90bf7

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread compiler/ml/translmod.ml
let has_exportable_module_path = function
| Some path -> (
match Path.flatten path with
| `Ok (_, _ :: _) -> true

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Reject hoists hidden by signature coercion

When a .resi or an explicit module signature omits the annotated function (or its enclosing module), this check still permits the annotation because it considers only the syntactic path. Signature coercion subsequently removes the field, and js_hoisted_aliases scans only exported module blocks, so compilation succeeds without producing either the flat export or a misplaced-attribute warning. Validate actual post-coercion visibility or diagnose marked bindings for which no alias was generated.

Useful? React with 👍 / 👎.

Comment thread compiler/ml/translcore.ml Outdated
else
Location.prerr_warning loc
(Warnings.Misplaced_attribute hoisted_function_attr_name)
| _ -> ())

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Diagnose unsupported function-binding patterns

For a function RHS bound with another irrefutable pattern, such as @res.hoistedFunction let (f as g) = () => "ok", the typed pattern is an alias around a variable rather than either accepted form. This fallback silently ignores the attribute, emitting neither a flat export nor the misplaced-attribute warning; reject unsupported patterns explicitly instead of treating the attribute as handled.

AGENTS.md reference: AGENTS.md:L43-L43

Useful? React with 👍 / 👎.

cknitt added 2 commits August 22, 2026 17:14
Signed-off-by: Christoph Knittel <ck@cca.io>
Signed-off-by: Christoph Knittel <ck@cca.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant